package io.github.talelin.latticy.controller.cms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TestController {

    public static void main(String[] args) {
       /** HashMap<String, Object> map = new HashMap<>();
        map.put("name", "赣南脐橙");
        map.put("amount", 9.9);
        map.put("description", "ok");
        map.put("count", 1);
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("name", "赣南脐橙");
        map2.put("amount", 9.9);
        map2.put("description", "ok");
        map2.put("count", 1);
        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("name", "红心木瓜");
        map3.put("amount", 19.9);
        map3.put("description", "ok");
        map3.put("count", 2);

        ArrayList<Map<String,Object>> list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        list.add(map3);
        System.out.println(list);


        List<Map<String,Object>> post_payments = new ArrayList<>(list.size());
        for (Map map4 : list) {
            System.out.println(map4);
            post_payments.add(new HashMap<String, Object>(){{
                put("name",map4.get("name"));
                put("amount", map4.get("amount"));
                put("description", map4.get("description"));
                put("count", map4.get("count"));
            }});
        }
        System.out.println(post_payments);*/

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name", "赣南脐橙");
        map.put("amount", 334);
        map.put("description", "ok");
        map.put("count", 2);
        list.add(map);
        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("name", "赣南脐橙");
        map2.put("amount", 41);
        map2.put("description", "ok");
        map2.put("count", 1);
        list.add(map2);
        Map<String,Object> map3 = new HashMap<String, Object>();
        map3.put("name", "红心木瓜");
        map3.put("amount", 199);
        map3.put("description", "ok");
        map3.put("count", 2);
        list.add(map3);
        Map<String,Object> map4 = new HashMap<String, Object>();
        map4.put("name", "黑心木瓜");
        map4.put("amount", 299);
        map4.put("description", "ok");
        map4.put("count", 2);
        list.add(map4);
        list = list.stream().collect(Collectors.groupingBy(d -> d.get("name"))).values().stream().map(d -> {
            Map<String, Object> sampleData = d.get(0);
           // sampleData.put("amount", d.stream().map(s -> new BigDecimal(s.get("amount").toString())).reduce(BigDecimal.ZERO, BigDecimal::add));

            //sampleData.put("count", d.stream().map(s -> new BigDecimal(s.get("count").toString())).reduce(BigDecimal.ZERO, BigDecimal::add));

            sampleData.put("amount", d.stream().mapToInt((s) -> Integer.valueOf(String.valueOf(s.get("amount")))).sum());
            sampleData.put("count", d.stream().mapToInt((s) -> Integer.valueOf(String.valueOf(s.get("count")))).sum());
            return sampleData;
        }).collect(Collectors.toList());
        System.out.println(list);
        System.out.println(list.stream().mapToInt((s) -> Integer.valueOf(String.valueOf(s.get("amount")))).sum());


    }
}
